var id = getUrlParam("id");
var data;
var ctime;
$.ajax({
  url: "/orders/get/" + id,
  async: false,
  dataType: "json",
  success: function(res) {
    data = res.result;
    ctime = data.ctime;
    data.ctime = data.ctime == null ? '' : new Date(data.ctime).format('yyyy-MM-dd h:m:s');
  }
});
var tpl = document.getElementById("tpl");
if (data.ordersState == 1) {
  var html = template("noPay", {});
  tpl.innerHTML = html;
  document.getElementById("cancel").onclick = function() {
    var btnArray = ['是', '否'];
    mui.confirm('您确认要取消订单？', '取消订单', btnArray, function(e) {
      if (e.index == 0) {
        updateState(7);
      }
    })

  };
}
if (data.ordersState == 2) {

  var html = template("payed", {});
  tpl.innerHTML = html;
  //var ctime = Date.parse(new Date());
  leftTimer(ctime + 4 * 60 * 60 * 1000);
  //console.log(Date.parse(new Date()));
}

if (data.ordersState == 3) {
  var html = template("use", {});
  tpl.innerHTML = html;
  document.getElementById("refund").onclick = function() {
    applyRefund();
  };

  var qrcode = new QRCode('qcode', {
    text: 'http://www.huanju100.cn/admin/m/code?id=' + id,
    width:100,
    height:100,
    colorDark: '#000000',
    colorLight: '#ffffff',
    correctLevel: QRCode.CorrectLevel.H
  });

}

if (data.ordersState == 4) {
  var html = template("used", {});
  tpl.innerHTML = html;
}

if (data.ordersState == 5) {
  var html = template("refunding", {});
  tpl.innerHTML = html;
}

if (data.ordersState == 6) {
  var html = template("refunded", {});
  tpl.innerHTML = html;

}

if (data.ordersState == 7) {
  var html = template("cancelTpl", {});
  tpl.innerHTML = html;
}

if (data.ordersState == 8) {
  var html = template("refundfail", {});
  tpl.innerHTML = html;
  document.getElementById("refund").onclick = function() {
    applyRefund();
  };
}



var main = document.getElementById("main");
var html = template("mainTpl", data);
main.innerHTML = html;

function applyRefund() {
  var btnArray = ['确定', '取消'];
  mui.prompt('请输入申请理由', '', '申请退款', btnArray, function(e) {
    if (e.index == 0) {
      $.ajax({
        url: "/orders//refund",
        data:{
          spid:id,
          refundReason:e.value
        },
        dataTpe:'post',
        async: false,
        dataType: "json",
        success: function(res) {
          if (res.code == 200) {
            mui.toast("操作成功");
            setTimeout(function() {
              location.reload();
            }, 1000);

          }
        }
      });
    } else {

    }
  })
}

function updateState(state) {
  $.ajax({
    url: "/orders/update?spid=" + id + "&state=" + state,
    async: false,
    dataType: "json",
    success: function(res) {
      if (res.code == 200) {
        mui.toast("操作成功");
        setTimeout(function() {
          location.reload();
        }, 1000);

      }
    }
  });
}

function leftTimer(timestamp) {
  var leftTime = timestamp - Date.parse(new Date()); //计算剩余的毫秒数
  var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
  var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
  var minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
  var seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
  days = checkTime(days);
  hours = checkTime(hours);
  minutes = checkTime(minutes);
  seconds = checkTime(seconds);
  if(seconds>=0){
    setInterval("leftTimer(" + timestamp + ")", 1000);
    document.getElementById("downTime").innerHTML = hours + ":" + minutes + ":" + seconds;
  }
}

function checkTime(i) { //将0-9的数字前面加上0，例1变为01
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function check(id, type) {
  var base = "http://" + document.domain + ":8080/";
  if (type == "场地") {
    var url = base + "site/detail?id=" + id;
  }
  if (type == "聚会套餐") {
    var url = base + "meal/detail?id=" + id;
  }
  if (type == "二级服务") {
    var url = base + "service/detail?sid=" + id;
  }

  location.href = url;
}
